package cn.zyk.moudle.account.controller;

import cn.zyk.moudle.account.dto.*;
import cn.zyk.result.*;
import cn.zyk.util.*;
import jakarta.servlet.http.*;
import lombok.extern.slf4j.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author eHlw
 * @Date 2024/9/13 20:49
 * @description:
 */

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private HttpServletRequest request;

    @PostMapping("/login")
    private Response<Map<String, String>> login(@ModelAttribute  UserDto userDto) {
        Map<String, String> loginData = new HashMap<>();
        loginData.put("eHlwToken", JwtUtil.createToken(userDto));
        return Response.ok(200, "登录成功", loginData);
    }

    @PostMapping("/add")
    private Response<UserDto> add(@RequestBody UserDto userDto) {
        String username = (String) request.getAttribute("username");
        String passwd = (String) request.getAttribute("passwd");
        log.info("當前登錄用戶為:{}，{}", username, passwd);
        return Response.ok(200, "用户添加成功", userDto);
    }


    @PostMapping("/upload")
    private Response<String> upload(@RequestParam String fileName) {
        System.out.println(fileName);
        return Response.ok(fileName);
    }


    @GetMapping("/getToken")
          private Response<Map<String, String>> getToken(@RequestParam  String username) {
        UserDto userDto =new UserDto();
        userDto.setUsername(username);
            Map<String, String> loginData = new HashMap<>();
            loginData.put("eHlwToken", JwtUtil.createToken(userDto));
            return Response.ok(200, "登录成功", loginData);
        }
}